﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Data.Entity;

using BO;
using BrightIdeasSoftware;
using UI.Core;
namespace UI
{
    public partial class ProductForm :EditFormBase,IMenuForm
    {
        public ProductForm()
        {
            InitializeComponent();

            MyGrid = grid;
        }

        protected override object OnAdd()
        {
            var obj = new Product();
            return obj;

        }
      

        private void ProductForm_Load(object sender, EventArgs e)
        {
            OnSetGridColumns();
            InitGrid();
            OnQuery();
            MyGrid.RowObjectType = typeof(Product);
        }
        ProductList mySource;
       
        protected override void FetchData()
        {
            mySource = ListFactory<ProductList>.GetList("");
            GridDataSource = mySource;
    
        }
        protected override void DataBind()
        {
            base.DataBind();
        
        }
        void OnSetGridColumns()
        {
            var cols = new List<OLVColumn>();

            cols.Add(new OLVColumn("产品名称", "Name") { Width = 250 });
            cols.Add(new OLVColumn("产品价格", "Price") { Width = 150 });
            cols.Add(new OLVColumn("产品类型名称", "ProductGroupName") { Width = 250 });

            MyGrid.Columns.AddRange(cols.ToArray());


        }

        protected override void OnSave()
        {
            try
            {
                foreach (Product delObj in RemovedList)
                {
                    if (delObj == null)
                        continue;
                     if (delObj.ID == 0)
                        continue;
                    mySource.Remove(delObj);
                }
                RemovedList.Clear();

                foreach (Product obj in MyGrid.Objects)
                {
                    if (obj == null)
                        continue;
                     if (obj.ID == 0)
                        mySource.Add(obj);

                }

                mySource.Save();
                AppContext.RefreshProductList();
                OnQuery();
            }
            catch (ExceptionEx exp)
            {
                ExcptionHandler.ShowExcption(exp);
            }

        }
    }
}
